<template>
  <div>
    <el-form ref="sRef" :inline="true" :model="deptCl.detpS" class="demo-dynamic">
      <el-form-item label="公司名称" prop="companyName">
        <el-input v-model.string="deptCl.detpS.companyName" autocomplete="off" placeholder="公司名称" type="text" />
      </el-form-item>
      <el-form-item label="负责人：" prop="leaderName">
        <el-input v-model="deptCl.detpS.leaderName" placeholder="负责人" type="text"></el-input>
      </el-form-item>
      <el-form-item label="部门名称" prop="deptName">
        <el-input v-model.string="deptCl.detpS.deptName" autocomplete="off" placeholder="部门名称" type="text" />
      </el-form-item>
      <el-form-item label="负责人" prop="leaderName">
        <el-input v-model.string="deptCl.detpS.leaderName" autocomplete="off" placeholder="负责人" type="text" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchDeptSubmit()">搜索</el-button>
        <el-button type="primary" @click="resetS(sRef)">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div>
    <el-row :gutter="20">
      <el-col :span="6">
        <el-button type="primary" @click="opendialog()">
          <el-icon>
            <Plus />
          </el-icon>
          添加部门
        </el-button>
      </el-col>
    </el-row>
  </div>
  <div>
    <el-table :data="deptCl.tableData" :default-expand-all="false" row-key="id" stripe style="width: 100%">
      <el-table-column align="center" header-align="center" label="公司名称" prop="companyName" width="180" />
      <el-table-column align="center" header-align="center" label="部门名称" prop="deptName" width="180" />
      <el-table-column align="center" header-align="center" label="负责人" prop="leaderName" width="180" />
      <el-table-column align="center" header-align="center" label="电话" prop="phone" width="180" />
      <el-table-column align="center" header-align="center" label="邮箱" prop="email" width="180" />
      <el-table-column align="center" header-align="center" label="创建时间" prop="createTime" width="180" />
      <el-table-column align="center" header-align="center" label="是否工作" prop="work" width="180">
        <template #default="scope">
          <el-switch v-model="scope.row.work" :active-value="true" :inactive-value="false" active-text="是" inactive-text="否" inline-prompt @change="chengStatus(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" header-align="center" label="操作">
        <template #default="scope">
          <el-button link size="small" type="primary" @click="edit(scope.row.id)">编辑 </el-button>
          <el-popconfirm title="确定要删除吗?" @confirm="deleteDepts(scope.row.id)">
            <template #reference>
              <el-button link type="primary">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div>
    <el-dialog v-model="deptCl.dialogAddCompany" destroy-on-close title="添加部门" @close="resetDialog(addRef)">
      <el-form ref="addRef" :model="deptCl.addDept" :rules="deptCl.roles">
        <el-form-item label="公司" prop="companyId">
          <el-select v-model="deptCl.addDept.companyId" placeholder="请选择公司" @change="getDept">
            <el-option v-for="item in deptCl.selectCompanyData" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="父部门" prop="pid">
          <el-cascader v-model="deptCl.addDept.pid" :getCheckedNodes="true" :options="deptCl.selectDeptTree" :props="deptCl.props" clearable />
        </el-form-item>
        <el-form-item label="部门名称" prop="deptName">
          <el-input v-model="deptCl.addDept.deptName" type="text"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="deptCl.addDept.phone" type="text"></el-input>
        </el-form-item>
        <el-form-item label="负责人" prop="leader">
          <el-select v-model="deptCl.addDept.leader" placeholder="请选择负责人">
            <el-option v-for="item in userCl.userSelectList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="邮件" prop="email">
          <el-input v-model="deptCl.addDept.email" type="text"></el-input>
        </el-form-item>
        <el-form-item label="顺序" prop="orderNum">
          <el-input-number v-model="deptCl.addDept.orderNum" :min="1" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="saveDepts()">保存 </el-button>
          <el-button @click="deptCl.dialogAddCompany = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import { deptClass, deptInfo, selectDeptInfo } from '@/page/types/admin/dept'
import { userInfoClass } from '@/page/types/admin/user'
import { ElMessage, FormInstance } from 'element-plus/es'
import { deleteDept, deptPage, detailDept, saveDept, selectCompany, selectDeptBox, updateDept, userSelect } from '@/api/admin'
import { baseListInfo } from '@/page/types/base/baseList'
import { ref } from 'vue'

const deptCl = reactive(new deptClass())
const userCl = reactive(new userInfoClass())
const sRef = ref<FormInstance>()
const addRef = ref<FormInstance>()
const searchDeptSubmit = () => {
  deptPage(deptCl.detpS).then(res => {
    const data = res.data
    deptCl.tableData = data
  })
}
const chengStatus = (data: any) => {
  deptCl.addDept.work = data.work
  deptCl.addDept.id = data.id
  updateDept(deptCl.addDept).then(res => {
    searchDeptSubmit()
    ElMessage({
      message: '更新状态成功',
      type: 'success'
    })
  })
}
const opendialog = () => {
  deptCl.dialogAddCompany = true
  selectCompany(null).then(res => {
    const data = res.data as baseListInfo[]
    deptCl.selectCompanyData = data
  })
}
const getDept = (val: any) => {
  selectDeptBox({ companyId: val }).then(res => {
    const data = res.data as selectDeptInfo[]
    if (data) {
      deptCl.selectDeptTree = data
    } else {
      deptCl.selectDeptTree = [] as selectDeptInfo[]
    }
  })
  userSelect(val).then(res => {
    const data = res.data as baseListInfo[]
    if (data) {
      userCl.userSelectList = data
    } else {
      userCl.userSelectList = [] as baseListInfo[]
    }
  })
}
const resetS = (rest: FormInstance | undefined) => {
  if (!rest) return
  rest.resetFields()
  searchDeptSubmit()
}
const resetDialog = (rest: FormInstance | undefined) => {
  if (!rest) return
  rest.resetFields()
  deptCl.addDept = {} as deptInfo
}
onMounted(() => {
  searchDeptSubmit()
})
const saveDepts = () => {
  if (deptCl.addDept.allDept && deptCl.addDept.allDept.length > 0) {
    deptCl.addDept.pid = deptCl.addDept.allDept[deptCl.addDept.allDept.length - 1]
  }
  if (deptCl.addDept.id) {
    updateDept(deptCl.addDept).then(res => {
      searchDeptSubmit()
      deptCl.dialogAddCompany = false
      ElMessage({
        message: '更新部门信息成功',
        type: 'success'
      })
    })
  } else {
    saveDept(deptCl.addDept).then(res => {
      searchDeptSubmit()
      deptCl.dialogAddCompany = false
      ElMessage({
        message: '保存部门信息成功',
        type: 'success'
      })
    })
  }
}
const edit = (id: string) => {
  deptCl.dialogAddCompany = true
  selectCompany(null).then(res => {
    const data = res.data as baseListInfo[]
    deptCl.selectCompanyData = data
    selectDeptBox({}).then(res => {
      const data = res.data as selectDeptInfo[]
      deptCl.selectDeptTree = data
    })
    userSelect(null).then(res => {
      const data = res.data as baseListInfo[]
      userCl.userSelectList = data
    })
  })
  detailDept(id).then(res => {
    const data = res.data
    deptCl.addDept = data
  })
}
const deleteDepts = (id: string) => {
  deleteDept(id).then(res => {
    searchDeptSubmit()
    ElMessage({
      message: '删除部门信息成功',
      type: 'success'
    })
  })
}
</script>
<style lang="scss" scoped>
.el-select {
  width: 200px;
}
</style>
